﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomPoint : MonoBehaviour
{
    public Vector3 rangePoints;//生成的范围

    public List<GameObject> startPlanets;
    [HideInInspector]
    public List<NumType> Types;
    [HideInInspector]
    public List<GameObject> planetList;


    void Start()
    {
        planetList = new List<GameObject>();
        Types = new List<NumType>();
        print(startPlanets.Count);

        for (int k = 0; k < startPlanets.Count; k++)
        {
            Types.Add(startPlanets[k].GetComponent<NumType>());          
        }

        StartCoroutine(RandomCreatePoints());
    }

    public IEnumerator RandomCreatePoints()
    {
        for (int j = 0; j < startPlanets.Count-(3-DataCenter.Instance.TotalPlayer); j++)
        {
            for (int i = 0; i < Types[j].nums; i++)
            {

                Vector3 rangePoint = new Vector3();
                rangePoint.x = Random.Range(-rangePoints.x, rangePoints.x);
                rangePoint.y = Random.Range(-rangePoints.y, rangePoints.y);
                rangePoint.x = rangePoint.x * Mathf.Cos(rangePoint.x);
                rangePoint.y = rangePoint.y * Mathf.Sin(rangePoint.y);

                GameObject go = GameObject.Instantiate(startPlanets[j], rangePoint, Quaternion.identity);
                planetList.Add(go);
            }
        }
        yield return new WaitForSeconds(0.5f);
        foreach (var t in planetList)
        {
            Destroy(t.GetComponent<Rigidbody2D>());
            t.GetComponent<CircleCollider2D>().enabled = false;
        }
    }

    public float PointDistance(GameObject a, GameObject b)
    {
        float distance;
        Vector2 aPosition = a.transform.position;
        Vector2 bPosition = b.transform.position;
        float xDistance = (aPosition.x - bPosition.x) * (aPosition.x - bPosition.x);
        float yDistance = (aPosition.y - bPosition.y) * (aPosition.y - bPosition.y);
        distance = Mathf.Sqrt(xDistance + yDistance);
        return distance;
    }
}
